package com.smart_environmental_protection.controller;

import com.smart_environmental_protection.pojo.Order;
import com.smart_environmental_protection.pojo.Purpose;
import com.smart_environmental_protection.pojo.User;
import com.smart_environmental_protection.service.OrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.io.IOException;
import java.util.*;

@RestController
public class OrderController {
    @Autowired
    private OrderService orderService;

    @GetMapping("/system/order/list")
    public Map<String,Object> purList(@RequestParam Integer state,@RequestParam Integer userId){
        Map map=new HashMap<String,Object>();
        List<Order> orderList=orderService.OrderList(state,userId);
        if ((orderList!=null)&& orderList.size()!=0){
            map.put("success",true);
            map.put("msg","成功");
            map.put("code",200);
            map.put("rows",orderList);
        }else {
            map.put("success",false);
            map.put("msg","失败");
        }
        return map;
    }



    @GetMapping("/system/order/{id}")
    public Map<String,Object> SelOrder(@PathVariable Integer id){
        Map map=new HashMap<String,Object>();
        Order order=orderService.SelOrder(id);
        if ((order!=null)){
            map.put("success",true);
            map.put("msg","成功");
            map.put("code",200);
            map.put("data",order);
        }else {
            map.put("success",false);
            map.put("msg","失败");
        }
        return map;
    }




    @PutMapping("/system/order")
    @ResponseBody
    public Map<String,Object> UpdOrder(@RequestBody Order order){
        Map map=new HashMap<String,Object>();
        int count=orderService.UpdOrder(order);

        if (count > 0){
            map.put("success",true);
            map.put("msg","成功");
            map.put("code",200);
        }else {
            map.put("success",false);
            map.put("msg","失败");
        }
        return map;
    }





    @PostMapping("/system/Insert")
    public Map<String,Object> InsertOrder(@RequestBody Order order){
        Map map=new HashMap<String,Object>();
        int count=orderService.InsertOrder(order);
        if (count > 0){
            map.put("success",true);
            map.put("msg","成功");
            map.put("code",200);
            map.put("order",order.getId());
        }else {
            map.put("success",false);
            map.put("msg","失败");
        }
        return map;
    }


    @PostMapping({"/order/uploadheadicon"})
    @ResponseBody
    public Map<String, Object> getUserById(@RequestParam("file") MultipartFile uploadFile, @RequestParam("id") Integer id, HttpServletRequest req) {
        Map<String, Object> map = new HashMap();
        File dir = new File("D:\\uploadFiles");
        if (!dir.exists()) {
            dir.mkdirs();
        }

        try {
            String oldName = uploadFile.getOriginalFilename();
            String icon = UUID.randomUUID().toString() + oldName.substring(oldName.lastIndexOf("."), oldName.length());
            uploadFile.transferTo(new File(dir.getAbsolutePath() + File.separator + icon));
            icon = "/" + icon;
            Order order = this.orderService.SelOrder(id);
            order.setCoverUrl(icon);
            this.orderService.setIcon(order);
            map.put("success", true);
            map.put("msg", "成功");
            map.put("headiconpath", icon);
            return map;
        } catch (IOException var9) {
            map.put("success", false);
            map.put("msg", "失败");
            return map;
        }
    }



}
